﻿@{
    ViewBag.Title = "Management";
}
<script src="~/Scripts/jquery.hiMallDatagrid.js"></script>
<div class="container">
    @{var CheckService = ViewBag.Market != null;
    var CheckDate = false;
    if (CheckService)
    {
        CheckDate = ((Himall.Model.ActiveMarketServiceInfo)ViewBag.Market).MarketServiceRecordInfo.Max(a => a.EndTime) >= DateTime.Now.Date;
    }
    }
    <ul class="nav nav-tabs-custom clearfix">
        <strong>优惠券</strong>
        <li class="active"><a>所有优惠券</a></li>
        @if (CheckService && CheckDate)
        {
            <li><a href="@Url.Action("Add")">新增优惠券</a></li>
        } 
        <li class=""><a href="@Url.Action("BuyService")">服务购买</a></li>
        <em class="primary-btn">
            <span class="glyphicon glyphicon-question-sign"></span>
            <div class="primary">
                <h3>温馨提示：</h3>
                <span>*</span><p>显示店铺的优惠券列表</p>
            </div>
        </em>
    </ul>

    @if (CheckService && CheckDate)
    {
        <div class="search-box only-line">
            <form class="custom-inline">
                <div class="form-group">
                    <label class="label-inline" for="">搜优惠券</label><input type="text" id="CouponName" class="form-control input-ssm">
                </div>
                <button id="searchBtn" class="btn btn-primary btn-sm" type="button">搜 索</button>
            </form>
        </div>
    }
    @if (!CheckService || !CheckDate)
    {	
		<div class="form-horizontal">
			<div class="form-group">
				<p class="apply-primary"><span class="glyphicon glyphicon-info-sign"></span>您还未购买优惠券服务或者服务已到期，<a href="./BuyService">现在购买</a></p>
			</div>
		</div>
    }
    else
    {
        <div>
            <table class="table" id="list"></table>
        </div>
    }
</div>

<script type="text/javascript">
    function query() {
        //订单表格
        $("#list").hiMallDatagrid({
            url: './GetItemList',
            nowrap: false,
            rownumbers: true,
            NoDataMsg: '没有找到符合条件的优惠券',
            border: false,
            fit: true,
            fitColumns: true,
            pagination: true,
            idField: "Id",
            pageSize: 15,
            pagePosition: 'bottom',
            pageNumber: 1,
            queryParams: {},
            columns:
            [[
                { field: "Id", title: 'Id', hidden: true, width: 150 },
                {
                    field: "CouponName", title: "优惠名称", width: 200, align: "left",
                },
               { field: "Price", title: "价值", width: 80, align: "center" },
                { field: "PerMax", title: "领取限制", width: 70, align: "left" },
                { field: "OrderAmount", title: "使用条件", align: "left",width:90 },
                { field: "Num", title: "库存", width: 90, align: "left" },
                {
                    field: "Date", title: "有效期", align: "center",
                    formatter: function (value, row, index) {
                        var html = '<span>' + row.StartTime + "至" + row.EndTime + '</span>';
                        return html;
                    }
                },

                {
                    field: "Receive", title: "领取人/次", width: 100, align: "center",
                    formatter: function (value, row, index) {
                        var html = '<span>' + row.RecevicePeople + "/" + row.ReceviceNum + '</span>';
                        return html;
                    }
                },
            {
                field: "Used", title: "已使用", width: 50,
            },
            {
                field: "operation", operation: true, title: "操作",width:220,
                formatter: function (value, row, index) {
                    var id = row.Id.toString();
                    var now = "@DateTime.Now.Date".replace(/-/g, '/');
                    var html = ["<span class=\"btn-a\">"];
                    switch (row.WXAuditStatus) {
                        case 1:
                            html.push("<a href='./Receivers/" + id + "'>领取详情</a>");
                            html.push("<a href='./Detail/" + id + "'>查看</a>");
                            if (new Date(row.EndTime) > new Date(now)) {
                                html.push("<a class=\"good-check\" href=\"./Edit/" + id + "\">编辑</a>");
                                html.push("<a class=\"good-check\" onclick=\"Cancel(" + id + ")\">使失效</a>");
                            }
                            break;
                        case 0:
                            html.push("同步审核中...");
                            break;
                        case -1:
                            html.push("同步审核失败");
                            break;
                    }
                    html.push("</span>");
                    return html.join("");
                }
            }
            ]]
        });
    }

    $(function () {

        query();
        $("#AddItem").click(function () {
            location.href = "./Add";
        });

        $('#searchBtn').click(function (e) {
            searchClose(e);
            var CouponName = $.trim($('#CouponName').val());
            $("#list").hiMallDatagrid('reload',
                {
                    CouponName: CouponName,
                });
        });
    });

    function Cancel(id) {
        $.dialog.confirm('确定使该优惠券失效吗？', function () {
            var loading = showLoading();
            $.post("./Cancel", { couponid: id }, function (data) { loading.close(); $.dialog.tips(data.msg); query(); });
        });
    }
</script>